package cn.edu.hqu.experiment1.Services;

import cn.edu.hqu.experiment1.DTO.CommentDTO;
import cn.edu.hqu.experiment1.DTO.MovieDTO;
import cn.edu.hqu.experiment1.Entities.Comment;
import cn.edu.hqu.experiment1.Entities.Movie;
import cn.edu.hqu.experiment1.Repository.CommentRepository;
import cn.edu.hqu.experiment1.Repository.MovieRepository;
import cn.mikuhl.onlinereviewsplatform.Entities.*;
import cn.edu.hqu.experiment1.Exception.MovieNotFoundException;
import cn.mikuhl.onlinereviewsplatform.Repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MovieService {

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private CommentRepository commentRepository;

    public List<MovieDTO> getAllMovies() {
        List<Movie> movies = movieRepository.findAll();
        return movies.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    public MovieDTO getMovieById(Long id) {
        Movie movie = movieRepository.findById(id).orElseThrow(() -> new MovieNotFoundException("Movie not found"));
        return convertToDTO(movie);
    }

    public List<Movie> searchMovies(String title, String director) {
        if (title != null && director != null) {
            return movieRepository.findByTitleContainingAndDirectorContaining(title, director);
        } else if (title != null) {
            return movieRepository.findByTitleContaining(title);
        } else if (director != null) {
            return movieRepository.findByDirectorContaining(director);
        } else {
            return movieRepository.findAll();
        }
    }

    public CommentDTO submitComment(Long movieId, Comment comment) {
        Movie movie = movieRepository.findById(movieId).orElseThrow(() -> new MovieNotFoundException("Movie not found"));
        comment.setMovie(movie); // 设置电影
        comment.setCreatedAt(LocalDateTime.now()); // 设置创建时间
        Comment savedComment = commentRepository.save(comment);
        return convertToCommentDTO(savedComment);
    }

    public String uploadMoviePoster(Long movieId, MultipartFile file) throws IOException {
        Movie movie = movieRepository.findById(movieId).orElseThrow(() -> new MovieNotFoundException("Movie not found"));

        String fileName = file.getOriginalFilename();
        Path filePath = Paths.get("uploads", fileName);
        Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

        movie.setPosterUrl(filePath.toString());
        movieRepository.save(movie);

        return filePath.toString();
    }

    private MovieDTO convertToDTO(Movie movie) {
        List<CommentDTO> commentDTOs = movie.getComments().stream()
                .map(this::convertToCommentDTO)
                .collect(Collectors.toList());
        return new MovieDTO(movie.getId(), movie.getTitle(), movie.getDirector(), movie.getDescription(), movie.getPosterUrl(), commentDTOs);
    }

    private CommentDTO convertToCommentDTO(Comment comment) {
        return new CommentDTO(comment.getId(), comment.getUserId(), comment.getContent(), comment.getCreatedAt());
    }
}
